package com.zyd.service.log.impl;

import com.google.common.collect.Maps;
import com.zyd.context.HeaderContext;
import com.zyd.io.page.PageBean;
import com.zyd.io.page.PageRequestBean;
import com.zyd.service.log.LogService;
import com.zyd.util.SqlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class LogServiceImpl implements LogService {



    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public void addLog(String username,String operationalMatters) {

        if("".equals(username))
        {
            username=(String) HeaderContext.get().get("userid");
        }
        if("".equals(username)||username == null)
        {
            throw new RuntimeException("非法的请求");
        }
        String str = "insert into zydzn_log (id,username,OperationalMatters,OperationTime ) values (REPLACE(UUID(),\"-\",\"\"),'"+username+"','"+operationalMatters+"',localtimestamp())" ;
        jdbcTemplate.execute(str);
    }

    @Override
    public PageBean<Map<String, Object>> getLog(PageRequestBean request) {
        String username = (String) request.get("username");
        String startTime = (String) request.get("startTime");
        String endTime = (String) request.get("endTime");

        String selectSql ="SELECT b.username,a.OperationalMatters,a.OperationTime from zydzn_log a  LEFT JOIN zydzn_users b on a.username = b.id where 1 = 1 ";
        String countSql="SELECT count(1) from zydzn_log a  LEFT JOIN zydzn_users b on a.username = b.id where 1 = 1 ";
        ArrayList<Object> params = new ArrayList<>();
        if (!StringUtils.isEmpty(username)) {
            selectSql+=" and b.username like ? ";
            countSql+=" and b.username like ? ";
            params.add("%"+username+"%");
        }
        if (!StringUtils.isEmpty(startTime)) {
            selectSql+=" and a.OperationTime >= ? ";
            countSql+=" and a.OperationTime >= ? ";
            params.add(startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            selectSql+=" and a.OperationTime <= ? ";
            countSql+=" and a.OperationTime <= ? ";
            params.add(endTime);
        }
        Long totalRows = jdbcTemplate.queryForObject(countSql, Long.class, params.toArray(new Object[0]));
        String pageSql = SqlUtils.addPage(selectSql+" order by a.OperationTime desc ", request.getCurrentPage(), request.getPageSize());
        List<Map<String, Object>> result = jdbcTemplate.query(pageSql, (rs, rowNum) -> {
            Map<String, Object> ret = Maps.newHashMap();
            ret.put("username", rs.getString("username"));
            ret.put("operationalMatters", rs.getString("OperationalMatters"));
            ret.put("operationTime", rs.getString("OperationTime"));
            return ret;
        }, params.toArray(new Object[0]));
        PageBean<Map<String, Object>> pageBean = new PageBean<Map<String, Object>>(request.getCurrentPage(),
                request.getPageSize(), totalRows.intValue());
        pageBean.setItems(result);
        return pageBean;
        /**
         * select * from (SELECT b.username,a.OperationalMatters,a.OperationTime from zydzn_log a  LEFT JOIN zydzn_users b on a.username = b.id where 1 = 1 order by a.OperationTime desc)  as a where 1=1 limit 0,20
         */
    }
}